package com.financial;

import com.alibaba.fastjson2.JSON;
import com.financial.common.utils.SecurityUtils;
import com.financial.transactionhistory.domain.FinancialTransaction;
import com.financial.transactionhistory.mapper.FinancialTransactionMapper;
import com.financial.transactionhistory.service.IFinancialTransactionService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 * discrb:
 *
 * @Author CD
 * @Date2025/6/15 13:33
 */

@SpringBootTest
public class text {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {
        System.out.println(redisTemplate);
    }

    @Test
    public void testString() {
        // 普通的set方法
        redisTemplate.opsForValue().set("name", "张三");
        System.out.println(redisTemplate.opsForValue().get("name"));
        // 设置带有过期时间的key
        redisTemplate.opsForValue().set("token", "123qweasd",20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        // setnx 当指定的键key不存在时,会将key的值设置为value，返回true，否则返回false(不能覆盖原有值)
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "09876", 5, TimeUnit.MINUTES));
        System.out.println(redisTemplate.opsForValue().setIfAbsent("lock", "34567", 5, TimeUnit.MINUTES));
    }

    @Test
    public void testHash() {
        // hash请求   大key 小key  value
        redisTemplate.opsForHash().put("user", "name", "张三");
        redisTemplate.opsForHash().put("user", "age", "30");
        // 根据大key和小key获取值
        System.out.println(redisTemplate.opsForHash().get("user", "name"));

        // 根据大key获取所有的小key
        Set<Object> keys = redisTemplate.opsForHash().keys("user");
        System.out.println(keys);

        // 根据大key获取所有的值
        List<Object> values = redisTemplate.opsForHash().values("user");
        System.out.println(values);

        // 删除小key和值
        redisTemplate.opsForHash().delete("user", "age");

    }

    @Test
    public void testList() {
        // 插入多个值[a,b,c]
        redisTemplate.opsForList().leftPushAll("mylist", "a", "b", "c");
        // 在列表左边插入一个值[d,a,b,c]
        redisTemplate.opsForList().leftPush("mylist", "d");
        // 获取列表中的数据
        System.out.println(redisTemplate.opsForList().range("mylist", 0, -1));
        // 从左边弹出一个，并获取值，弹出后列表中删除
        System.out.println(redisTemplate.opsForList().leftPop("mylist"));
        // 获取列表的长度
        System.out.println(redisTemplate.opsForList().size("mylist"));

    }


    @Autowired
    private FinancialTransactionMapper facade;
    @Test
    public void testSet() {
        //获取交易记录信息
        List<FinancialTransaction> financialTransactionList = facade.selectFinancialTransactionList(new FinancialTransaction());
        financialTransactionList.forEach(financialTransaction -> {
            //将交易记录信息保存到redis中
            System.out.println(financialTransaction);
            //把数据转化成json字符串
            String json = JSON.toJSONString(financialTransaction);
            System.out.println( json);
            redisTemplate.opsForHash().put("list", financialTransaction.getId().toString(), json);
        });
    }
    @Test
    public void testGetFinancialTransaction(){
//        return redisTemplate.opsForHash().entries("list",);
        //获取数据
//        String s  = (String) redisTemplate.opsForHash().get("list", "3");
        //转换成对象
//        FinancialTransaction financialTransaction = JSON.parseObject(s, FinancialTransaction.class);
        System.out.println(redisTemplate.opsForHash().entries("list"));
    }

    @Test
    public void testlist() {
        //获取交易记录信息
        List<FinancialTransaction> financialTransactionList = facade.selectFinancialTransactionList(new FinancialTransaction());
        financialTransactionList.forEach(financialTransaction -> {
            //将交易记录信息保存到redis中
            System.out.println(financialTransaction);
            //把数据转化成json字符串
            String json = JSON.toJSONString(financialTransaction);
            System.out.println( json);
            redisTemplate.opsForList().leftPush("SecurityUtils.getUserId()".toString(), json);
        });
    }

}
